package com.ml.mall.config;

import com.ml.mall.consts.SysConstants;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.utils.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;

import java.util.Optional;

/**
 * @description: TODO
 * @author kyyang
 * @date 2020/12/28 20:21
 * @version 1.0
 */
@Configuration
public class UserIDAuditorConfig implements AuditorAware<Long> {
    @Override
    public Optional<Long> getCurrentAuditor() {
        try {
            String token = HttpUtil.getRequest().getHeader("Authorization");
            if (StringUtils.isNotEmpty(token)) {
                return Optional.of(JwtUtil.getUserId(token));
            }
        } catch (Exception e) {
            //返回系统用户id
            return Optional.of(SysConstants.SYSTEM_USER_ID);
        }
        //返回系统用户id
        return Optional.of(SysConstants.SYSTEM_USER_ID);
    }
}
